// packageC/pages/package/agv_manage/getCode/getCode.js
const mybehavior = require("../../../../../behaviors/requests")
Page({

  /**
   * 页面的初始数据
   */
  behaviors: [mybehavior],
  data: {
    baseUrl: "https://logisticsengineering.gree.com/agv_dispatch_system",
    options1: [],
    options2: ['是', '否'],
    AGV_name: "",
    if_online: "",
    data_list: [],
    current: 1,
    page_size: 5,
    department: "",
    page_all: 0,
    total: 0,
    caozuo_show: false,
    change_agv_name: "",
    current_data: {},
    scan_scene: "",
    department_options: [],
    data_list: [],
    agv_options: [],
    agv_name_options: [],
    line_options: [],
    agv_num:"",
    route_id:"",

  },
  agvStart() {
    let url = this.data.baseUrl + "/select_route_and_start"
    let data = {
      agv_num: this.data.agv_num,
      route_id: this.data.route_id,
      status: true,
    }
    // console.log("传过去的数据", data);
    this.post(url, data).then(res => {
      if (res.code == 20000) {
        wx.showToast({
          title: res.msg,
          icon:'none'
        })
      } else {

        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    }).catch(err => {

      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },
  agvStop() {
    let url = this.data.baseUrl + "/select_route_and_start"
    let data = {
      agv_num: this.data.agv_num,
      route_id: this.data.route_id,
      status: false,
    }
    // console.log("传过去的数据", data);
    this.post(url, data).then(res => {
      if (res.code == 20000) {
        wx.showToast({
          title: res.msg,
          icon:'none'
        })
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    }).catch(err => {
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },
  // 查询表格数据
  getMagneticData() {
    let url = this.data.baseUrl + "/get_magnetic_route"
    let data = {
      department: this.data.department,
      current: 1,
      page_size: 9999,
    }
    // console.log("传过去的数据", data);
    this.get(url, data).then(res => {
      if (res.code == 20000) {
        this.setData({
          data_list: res.data.table_data,
        })

      } else {
        this.setData({
          data_list: [],
        })
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      this.setData({
        data_list: [],
      })
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })

  },
  // 查询小车信息
  getAgvtData() {
    let url = this.data.baseUrl + "/get_magnetic_agv_info"

    let data = {
      department: this.data.department,
    }
    this.get(url, data).then(res => {
      if (res.code == 20000) {
        let agv_name_options = []
        for (let i = 0; i < res.data.table_data.length; i++) {
          agv_name_options.push(res.data.table_data[i].name)
        }
        this.setData({
          agv_options: res.data.table_data,
          agv_name_options: agv_name_options,
        })
      } else {
        this.setData({
          options2: [],
        })
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      this.setData({
        options2: [],
      })
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })

  },
  // 查询部门
  getDepartmentData() {
    let url = this.data.baseUrl + "/get_department_list"

    let data = {
    }
    this.get(url, data).then(res => {
      if (res.code == 20000) {
        this.setData({
          department_options: res.data

        })

      } else {
        this.setData({
          department_options: [],
        })
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      this.setData({
        department_options: [],
      })
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })

  },
  // 下拉选择
  getPickerVlaue1(e) {
    let key = e.target.id
    console.log(e);
    this.setData({
      [key]: e.detail.name
    })
    this.getAllAgvData()
  },
  // 部门下拉选择
  getPickerVlaue(e) {
    let key = e.target.id
    this.setData({
      [key]: e.detail
    })
    this.getAgvtData()
    this.getMagneticData()

  },
  // 车辆下拉选择
  getPickerVlaue2(e) {
    let key = e.target.id
    this.setData({
      [key]: e.detail.name,
      agv_num:e.detail.agv_num
      
    })
    let line_list = this.data.data_list
    let line_options = []
    for (let i = 0; i < line_list.length; i++) {
      if (line_list[i].special_agv_list.includes(e.detail.key)) {
        line_options.push(line_list[i])
      }

    }
    console.log("xxx", line_options);
    this.setData({
      line_options: line_options
    })
  },
  // 路线下拉选择
  getPickerVlaue3(e) {
    let key = e.target.id
    console.log(e);
    this.setData({
      [key]: e.detail.magnetic_route_name,
      route_id: e.detail.key,
    })

  },
  // 获取小车列表
  getAllData() {
    let url = this.data.baseUrl + "/get_agv_num_list"
    let data = {
    }
    // console.log("传过去的数据", data);
    this.get(url, data).then(res => {
      if (res.code == 20000) {
        if (this.data.scan_scene != "") {
          let lists = res.data.filter(item => item.agv_num == this.data.scan_scene)
          console.log(lists);
          if (lists.length != 0) {
            this.setData({
              options1: res.data,
              AGV_name: lists[0].name,
            })
            this.getAllAgvData()
          } else {
            this.setData({
              options1: res.data,
            })
          }

        } else {
          this.setData({
            options1: res.data,
          })
        }
      } else {
        this.setData({
          options1: [],
        })
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      this.setData({
        options1: [],
      })
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })

  },

  // 查询小车信息
  getAllAgvData() {
    let url = this.data.baseUrl + "/get_magnetic_agv_info"
    let data = {
      department: "",
      if_online: "",
      AGV_name: this.data.AGV_name,
      current: 1,
      page_size: 999,
    }
    this.get(url, data).then(res => {
      if (res.code == 20000) {
        console.log(res);
        this.setData({
          row_data: res.data.table_data[0]
        })

      } else {

        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {

      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })

  },


  top(e) {
    console.log("上");
  },

  left(e) {
    console.log("左");
  },

  bottom(e) {
    console.log("下");
  },

  right(e) {
    console.log("右");
  },
  stop() {
    console.log("停");

  },

  /**
   * 生命周期函数--监听页面加载
   */
  // 生成小程序码

  onLoad(options) {
    this.getDepartmentData()
    const scene = decodeURIComponent(options.scene)
    console.log(scene);
    let self = this
    if (scene != "undefined") {
      self.setData({
        scan_scene: scene,
      })

      console.log(scene);
    }
    self.getAllData()

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})